import java.util.HashMap;
import java.util.Map;

public class _5totalFruit {
    //水果成篮
    public int totalFruit(int[] fruits) {
    Map<Integer,Integer> hash = new HashMap<Integer,Integer>();
    int left = 0,right = 0,len = 0;
    while(right < fruits.length){
        int in = fruits[right];
        hash.put(in,hash.getOrDefault(in,0)+1);
        while(hash.size() > 2){
            int out = fruits[left];
            hash.put(out,hash.get(out)-1);
            if(hash.get(out) == 0)
                hash.remove(out);

            left++;
        }

        len = Math.max(len,right-left+1);
        right++;
    }
    return len;
}
}
